package com.example.demo.result;


import com.alibaba.fastjson.JSONObject;
import com.example.demo.exception.SXException;
import com.example.demo.exception.SXExceptionEnum;
import lombok.Data;

import java.util.List;

/**
 * rest 返回值
 *
 * @param <T>
 */
@Data
public class RestResponse<T> {
    private int statusCode; //code全平台唯一
    private String msg = "";
    private T data;

    public RestResponse(SXExceptionEnum argumentError, String message, List<String> msgList) {
        super();
    }

    public RestResponse(T data) {
        if (data instanceof SXException) {
            this.msg = ((SXException) data).getMessage();
            this.statusCode = ((SXException) data).getCode();
        } else {
            this.data = data;
        }
    }

    public RestResponse(int statusCode, String msg) {
        this(statusCode, msg, null);
    }


    public RestResponse(int statusCode, String msg, T data) {
        super();
        this.statusCode = statusCode;
        this.msg = msg;
        this.data = data;
    }

    public RestResponse(int statusCode, String msg, T result, T data) {
        super();
        this.statusCode = statusCode;
        this.msg = msg;
        this.data = data;
    }

    @Override
    public String toString() {
        return "RestResponse [statusCode=" + statusCode + ", msg=" + msg + ", data=" + JSONObject.toJSONString(data) + "]";
    }


}
